package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/transactionManagerService"
	"hc_server/runtime/e"
)

type TransactionManagerListParam struct {
	Index any `json:"index"`
	Size  any `json:"size"`
	Sort  any `json:"Sort,omitempty"` //排序:1=总额降序,2=总额升序,3=数量降序,4=数量升序
}

// TransactionManagerMerchantList 交易管理商户列表
func TransactionManagerMerchantList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TransactionManagerListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	transactionService := transactionManagerService.TransactionManager{
		AgentId: cast.ToInt(userId),
		Index:   cast.ToInt(param.Index),
		Size:    cast.ToInt(param.Size),
		Sort:    cast.ToInt(param.Sort),
	}
	result, err := transactionService.TransactionManagerMerchantList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TransactionManagerTransactionListParam struct {
	MerchantID any `json:"merchantID"`
	Index      any `json:"index"`
	Size       any `json:"size"`
}

func TransactionManagerTransactionList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TransactionManagerTransactionListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transactionService := transactionManagerService.TransactionManager{
		MerchantId: cast.ToInt(param.MerchantID),
		Index:      cast.ToInt(param.Index),
		Size:       cast.ToInt(param.Size),
	}
	result, err := transactionService.TransactionManagerTransactionList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}

type TransactionManagerTransactionInfoParam struct {
	Id any `json:"id"`
}

func TransactionManagerTransactionInfo(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   TransactionManagerTransactionInfoParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	transactionService := transactionManagerService.TransactionManager{
		MerchantId: cast.ToInt(param.Id),
	}
	result, err := transactionService.TransactionManagerTransactionInfo()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}
	appG.ResponseSuccess("成功", result)
}
